package com.wjc.train.business.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wjc.train.business.entity.TrainStation;
import com.wjc.train.business.repository.TrainStationRepository;
import com.wjc.train.business.req.TrainStationQueryReq;
import com.wjc.train.business.req.TrainStationSaveReq;
import com.wjc.train.business.resp.TrainStationQueryResp;
import com.wjc.train.business.service.ITrainStationService;
import com.wjc.train.common.enums.LogicEnum;
import com.wjc.train.common.exception.BusinessException;
import com.wjc.train.common.untils.CopyUtil;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 车站表 服务实现类
 * </p>
 *
 * @author Guessed
 * @since 2023-08-02
 */
@Service
public class TrainStationServiceImpl  implements ITrainStationService {

    @Resource
    private TrainStationRepository trainstationRepository;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(TrainStationSaveReq trainstationSaveReq) {
        TrainStation dbTrainStation =
                trainstationRepository.getOne(new QueryWrapper<TrainStation>()
                        .eq("train_code", trainstationSaveReq.getTrainCode())
                        .eq("name",trainstationSaveReq.getName())
                        .eq("is_deleted", LogicEnum.NOT_DELETED.getCode()));
        if(ObjectUtil.isNotEmpty(dbTrainStation)){
            throw new BusinessException("同车次站名已存在");
        }
        TrainStation trainstation = CopyUtil.copy(trainstationSaveReq, TrainStation.class);
        trainstationRepository.saveOrUpdate(trainstation);
    }

    @Override
    public IPage<TrainStationQueryResp> queryList(IPage<TrainStationQueryResp> page, TrainStationQueryReq trainstationQueryReq) {
        return trainstationRepository.queryList(page,trainstationQueryReq);
    }

    @Override
    public void delete(Long id) {
        trainstationRepository.getBaseMapper().deleteById(id);
    }

}
